<?php
namespace app\admin\model;

use think\Model;

class User extends Model
{
	//设置主键
    protected $pk = 'id';
    // 设置当前模型对应的完整数据表名称
    protected $table = 'super_user';
	protected function initialize()
    {
        //需要调用`Model`的`initialize`方法
        parent::initialize();;
        //TODO:自定义的初始化
    }
	// 实例化模型
	public function getUserPage($where,$order,$arr,$pagesize=12){
		$db = new User();
        return $db->where($where)->order($order)->paginate($pagesize,false,['query'=>$arr]);
	}
    public function getUserCount($where,$order){
		$db = new User();
        $vn = $db->where($where)->order($order)->select();
        return [
             'data'=>$vn,
             'count'=>count($vn),
             ];

	}
    public function gerUserInfo($id){
        $db = new User();
        $vn = $db->where(array('id'=>$id))->find();
		$vm = getUserCardInfo($vn['User_id']);
		$vn['friend'] = getUserFriendInfo($vn['User_id'],'Friend_ids');
		$vn['Blacklist'] = getUserFriendInfo($vn['User_id'],'Blacklist_ids');
		$vn['Stranger'] = getUserFriendInfo($vn['User_id'],'Stranger_ids');
		$vn['friend_num'] = count($vn['friend']);
		$vn['Blacklist_num'] = count($vn['Blacklist']);
		$vn['Stranger_num'] = count($vn['Blacklist']);
		if($vm['Poster']){
			$vm['Poster'] = json_decode($vm['Poster']);
		}
		if($vm['Hobby']){
			$vm['Hobby'] = json_decode($vm['Hobby']);
		}
        $vn['card'] = $vm;
		//print_r($vn);exit;
        return $vn;
    }
	// 审核用户
	public function toexamineUser($id,$sta){
		$db = new User();
		$sta = $sta == 1?2:1;
		if($db->where(array('id'=>$id))->setField('State',$sta))
		return json(['code'=>1,'msg'=>'更新成功']);
		return json(['code'=>0,'msg'=>'更新失败']);
	    
	}
	// 修改用户
	public function editUser($id,$nme,$sto){
		$db = new User();
		$sto = $sto?$sto:0;
		if($db->where(array('id'=>$id))->update(array('Name'=>$nme,'Sort'=>$sto)))
		return json(['code'=>1,'msg'=>'修改成功']);
		return json(['code'=>0,'msg'=>'修改失败']);
	    
	}
	//模糊查询用户ID
	public function queryUserIds($nme,$val='User_id')
	{
		$db = new User();
		$where['Username'] = ['like','%'.$nme.'%'];
		$arr = $db->where($where)->field($val)->select();
		$ids = array_column($arr,$val);
		return implode(',',$ids);
	}
}